package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author:hujunwei
 * @description:角色控制器
 * @date:2019/7/29
 * @version:1.0
 */
@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /*
     * @Author:hujunwei
     * @功能:查询所有角色信息，用于页面回显
     * @date: 2019/7/29
     * @param
     * @return com.itheima.health.entity.Result
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> RoleList = roleService.findAll();
            return new Result(true,"查询角色信息成功",RoleList);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"查询角色信息失败");
        }
    }

    /*
     * @Author:hujunwei
     * @功能:角色分页查询
     * @date: 2019/7/29
     * @param pageBean
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        PageResult pageResult = null;
        try{
            pageResult = roleService.pageQuery(pageBean.getCurrentPage(),pageBean.getPageSize(),pageBean.getQueryString());
        }catch(Exception e){
            e.printStackTrace();
            pageResult = new PageResult(0L,new ArrayList());
        }
        return pageResult;
    }

    /*
     * @Author:hujunwei
     * @功能:新增角色，同时新增角色与权限的中间表
     * @date: 2019/7/29
     * @param role
     * @param permissionIds
     * @return com.itheima.health.entity.Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] menuIds,Integer[] permissionIds){
        return   roleService.add(role,menuIds,permissionIds);
    }

    /*
     * @Author:hujunwei
     * @功能:基于Id删除角色
     * @date: 2019/7/30
     * @param id
     * @return com.itheima.health.entity.Result
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
          return   roleService.deleteById(id);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try{
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true,"编辑角色成功");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"编辑角色失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        return   roleService.findById(id);
    }

}
